/**
  ******************************************************************************
  * @file    bsp_usart_ex.h
  * @author  Iron
  * @date    2018-08-08
  * @version v1.0
  * @brief   bsp_usart_ex header file
  */

#ifndef __BSP_USART_EX_H
#define __BSP_USART_EX_H

#ifdef __cplusplus
extern "C" {
#endif

/* includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "event_groups.h"
#include "bsp_usart.h"

/* exported types ------------------------------------------------------------*/
#define BSP_USART_RX_EVT        0x00001
#define BSP_USART_TXC_EVT       0x00002

typedef void (*usart_ex_isr_rx_indicate_t)(void *handle, uint32_t length, BaseType_t *pxHigherPriorityTaskWoken);
typedef void (*usart_ex_isr_tx_complete_t)(void *handle, BaseType_t *pxHigherPriorityTaskWoken);

typedef struct
{
    bsp_usart_id_t uart;
    EventGroupHandle_t evt_handle;
    StaticEventGroup_t evt;
    usart_ex_isr_rx_indicate_t rx_indicate;
    usart_ex_isr_tx_complete_t tx_complete;
} bsp_usart_handle_t;

/* exported constants --------------------------------------------------------*/
/* exported macro ------------------------------------------------------------*/
/* exported functions ------------------------------------------------------- */
int32_t bsp_usart_ex_init(bsp_usart_handle_t *husart, bsp_usart_id_t uart, uint32_t baudrate,
                          usart_ex_isr_rx_indicate_t rx_indicate, usart_ex_isr_tx_complete_t tx_complete);
int32_t bsp_usart_send_ex(bsp_usart_handle_t *husart, uint8_t *data, int32_t len, uint32_t timeout);

int32_t bsp_usart_getbyte_ex(bsp_usart_handle_t *husart, uint8_t *data, uint32_t timeout);
int32_t bsp_usart_recv_ex(bsp_usart_handle_t *husart, uint8_t *data, int32_t size, uint32_t timeout);
int32_t bsp_usart_get_rx_buf_len_ex(bsp_usart_handle_t *husart);

// The function caller take the responsibility to keep "data" safe!
int32_t bsp_usart_send_tx_no_wait(bsp_usart_handle_t *husart, uint8_t *data, int32_t len);

/**
  * @}
  */

#ifdef __cplusplus
}
#endif

#endif /* __BSP_USART_EX_H */

/******************* (C)COPYRIGHT 2018 ***** END OF FILE *********************/
